package com.example.zuoye4.mapper;

import com.example.zuoye4.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {
    // 查询所有学生信息
    @Results(id = "studentMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "number", column = "no"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "qq", column = "qq")
    })
    @Select("select * from tb_student")
    List<Student> getAll();

    // 根据学号查询学生信息
    @ResultMap("studentMap")
    @Select("select * from tb_student where no = #{no}")
    Student getStudentByNo(String no);

    // 插入学生信息
    @Insert("insert into tb_student(no, name, sex, phone, qq) values(#{number}, #{name}, #{sex}, #{phone}, #{qq})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, before = false)
    int addStudent(Student student);

    // 根据学号删除学生信息
    @Delete("delete from tb_student where no = #{no}")
    int deleteStudentByNo(String no);

    // 根据学号更新学生信息
    @Update("update tb_student set name = #{name}, sex = #{sex}, phone = #{phone}, qq = #{qq} where no = #{number}")
    int updateStudent(Student student);

    // 根据学号或姓名查询学生信息
    @ResultMap("studentMap")
    @Select("<script>select * from tb_student where 1=1 " +
            "<if test='number != null'>and no = #{number}</if>" +
            "<if test='name != null'>and name = #{name}</if>" +
            "</script>")
    List<Student> getStudentByNumberOrName(Student student);
}




